package org.example.Demo;

import sun.security.util.Password;

import java.sql.*;

public class JDBCDemo_prepareStatement {
    public static void main(String[] args) throws Exception {

        /*
        * prepareStatement 使用示例
        * */
        String url = "jdbc:mysql://127.0.0.1:3306/firstservlet";
        String username = "root";
        String password = "Abc123+++";
        Connection conn = DriverManager.getConnection(url, username, password);

        //定义sql
        int id = 1;
        String name = "张三";
        String sql = "select * from account where id = ? and name = ?";
        //获取PreparedStatement对象
        PreparedStatement preparedStatement = conn.prepareStatement(sql);

        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);

        ResultSet res = preparedStatement.executeQuery();

        while (res.next()){
            int res_id = res.getInt(1);
            String res_name = res.getString(2);
            int money = res.getInt(3);
            System.out.println(res_id + " " + res_name + " " + money);
        }

        //释放资源
        conn.close();;
        preparedStatement.close();
        res.close();

    }
}
